package Study_6;

import java.util.Scanner;

/**
 * @author: zxy
 * @date: 2024/5/17 19:37
 * @version: 1.0
 */
public class CustomerView {
    Scanner scan = new Scanner(System.in);
    // 限定最多储存用户个数为10
    private CustomerList customerList = new CustomerList(10);

    public void enterMainMenu(){
        boolean flag = true;
        while(flag) {
            System.out.println("\n-----------------电商客户管理系统--------------\n");
            System.out.println("                 1. 添 加 客 户");
            System.out.println("                 2. 修 改 客 户");
            System.out.println("                 3. 删 除 客 户");
            System.out.println("                 4. 客 户 列 表");
            System.out.println("                 5. 退 出");
            System.out.print("\n                 请选择（1-5）：");
            int choice = scan.nextInt();
            switch (choice){
                case 1:{
                    addNewCustomer();
                    break;
                }
                case 2:{
                    modifyCustomer();
                    break;
                }
                case 3:{
                    deleteCustomer();
                    break;
                }
                case 4:{
                    ListAllCustomers();
                    break;
                }
                case 5:{
                    System.out.println("是否退出（Yes/No）：");
                    char withDraw = scan.next().charAt(0);
                    if (withDraw == 'y'){
                        flag = false;
                        break;
                    }
                    break;
                }
                default:{
                    System.out.println("选择错误，请重新选择！");
                    break;
                }
            }
        }
    }

    public void addNewCustomer(){
        Customer customer = new Customer();
        System.out.println("\n-----------------添 加 客 户--------------");
        System.out.print("姓名：");
        customer.setName(scan.next());

        System.out.print("性别：");
        customer.setGender(scan.next().charAt(0));

        System.out.print("年龄：");
        customer.setAge(scan.nextInt());

        System.out.print("电话：");
        customer.setPhone(scan.next());

        System.out.print("邮箱：");
        customer.setEmail(scan.next());

        customerList.addCustomer(customer);

        System.out.println("添加成功！");
    }

    public void modifyCustomer(){
        System.out.print("请输入所需要修改用户的编号：");
        int index = scan.nextInt() - 1;

        Customer cust = new Customer();
        System.out.println("\n-----------------修 改 客 户--------------");
        System.out.print("姓名：");
        cust.setName(scan.next());

        System.out.print("性别：");
        cust.setGender(scan.next().charAt(0));

        System.out.print("年龄：");
        cust.setAge(scan.nextInt());

        System.out.print("电话：");
        cust.setPhone(scan.next());

        System.out.print("邮箱：");
        cust.setEmail(scan.next());

        customerList.replaceCustomer(index,cust);
        System.out.println("替换成功！");

    }

    public void deleteCustomer(){
        System.out.println("\n-----------------删 除 客 户--------------");
        System.out.print("请输入所需要删除用户的编号：");
        int index = scan.nextInt() - 1;
        if(customerList.deleteCustomer(index)){
            customerList.setTotal(customerList.getTotal() - 1);
            System.out.println("删除成功！");
        }
    }

    public void ListAllCustomers(){
        System.out.println("\n-----------------客 户 列 表--------------");
        System.out.println("编号" +"\t" + "姓名" + "\t" + "性别" + "\t" + "年龄" + "\t" + "电话" +
                "\t\t\t" + "邮箱" + "\t\t\t");
        for (int i = 0; i < customerList.getTotal(); i++) {
            System.out.println((i+1) + "\t" + customerList.getAllCustomers()[i].getName() + "\t"
                    + customerList.getAllCustomers()[i].getGender() +  "\t"
                    + customerList.getAllCustomers()[i].getAge() + "\t"
                    + customerList.getAllCustomers()[i].getPhone() + "\t\t\t"
                    + customerList.getAllCustomers()[i].getEmail() + "\t\t\t");
        }
    }

    public static void main(String[] args){
        CustomerView customerView = new CustomerView();
        customerView.enterMainMenu();
    }
}
